<template>
  <div class="dialog">
    <el-dialog width="45%" title="修改站点支付方式" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="站点支付方式：" prop="payMethods">
          <el-col :span="16">
            <el-select v-model="form.payMethods" placeholder="请选择" multiple clearable>
              <el-option
                v-for="(item,index) in payMethodOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editPayMethod } from '@/api/enterprise'
import { PAY_METHOD_STATUS } from '@/utils/enum'
export default {
  name: 'PayMethod',
  props: {
    formDetail: {
      type: Object,
      default: () => ({})
    }
  },

  data() {
    return {
      payMethodOptions: PAY_METHOD_STATUS,
      loading: false,
      form: {
        payMethods: []
      },
      rules: {
        payMethods: [{ required: true, message: '请选择支付方式', trigger: ['change'] }]
      }
    }
  },

  created() {
    const { payMethods = [] } = this.formDetail
    this.form = {
      payMethods
    }
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.formDetail.id
          }
          editPayMethod(data).then(() => {
            this.$message.success('修改成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
